/*
 * Copyright (c) 2010 Andy Aspell-Clark
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */
package uk.org.aspellclark.agenedb.view.tree;

import java.util.List;

import uk.org.aspellclark.agenedb.R;
import uk.org.aspellclark.agenedb.view.GedcomTreeView;
import uk.org.aspellclark.agenedb.view.LayoutUtils;
import uk.org.aspellclark.gedcom.controller.Db4oController;
import uk.org.aspellclark.gedcom.controller.IndividualController;
import uk.org.aspellclark.gedcom.model.records.IndividualRecord;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * This class displays a single individual as a box in a graph on screen which
 * the user can tap on to select.
 * 
 * @author andy.aspell-clark
 */
public class PersonChildsCntrl extends LinearLayout implements android.view.View.OnClickListener {

	private List<IndividualRecord> individuals;
	//private final int padding = 3;
	private final int RowPadding = 5;
	private int iconSize = 24;
	private int textSize = 18;
	private int textSizeSmall = 12;
	private boolean drawBorder = false;

	private IndividualController individualCntrlr;

	public PersonChildsCntrl(final Context context) {
		super(context);
		this.setOrientation(LinearLayout.VERTICAL);
		this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
		this.individualCntrlr = new IndividualController(Db4oController.getInstance());
	}

	public PersonChildsCntrl(final Context context, final AttributeSet attrs) {
		super(context, attrs);
		this.setOrientation(LinearLayout.VERTICAL);
		this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	}

	public PersonChildsCntrl(final Context context, boolean drawBorder) {
		super(context);
		this.drawBorder = drawBorder;
		this.setOrientation(LinearLayout.VERTICAL);
		this.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
	}

	private ImageView getGenderIcon() {
		ImageView genderIcon = new ImageView(this.getContext());

		genderIcon.setAdjustViewBounds(true);
		genderIcon.setScaleType(ImageView.ScaleType.FIT_CENTER);
		genderIcon.setMaxHeight(this.iconSize);
		genderIcon.setMaxWidth(this.iconSize);
		genderIcon.setPadding(this.RowPadding, this.RowPadding, this.RowPadding, this.RowPadding);
		LayoutUtils.Layout.WidthWrap_HeightWrap.applyTableRowParams(genderIcon);

		return genderIcon;

	}

	public List<IndividualRecord> getIndividuals() {
		return this.individuals;
	}

	@Override
	public void onClick(View v) {
		LinearLayout me = (LinearLayout) v;
		TextView tv = (TextView) me.getChildAt(1);
		String indivName = tv.getText().toString();

		Context ctx = this.getContext();
		GedcomTreeView agdb = (GedcomTreeView) ctx;

		IndividualRecord ind = this.individualCntrlr.getIndividual(indivName);
		agdb.updateCurrentPerson(ind.xref);
	}

	/**
	 * sets the individual that this control will display.
	 * 
	 * @param indiv
	 */
	public void setIndividuals(List<IndividualRecord> indivs) {

		Resources res = this.getContext().getResources();
		if (this.drawBorder) {
			Drawable bkgrnd = res.getDrawable(R.drawable.tree_vw_cldrn);
			this.setBackgroundDrawable(bkgrnd);
		}
		this.textSize = 14;
		this.textSizeSmall = (int) (this.textSize * 0.75);
		this.iconSize = 16;

		this.individuals = indivs;
		if (this.individuals.size() > 0) {
			for (IndividualRecord indiv : this.individuals) {
				ImageView genderIcon = this.getGenderIcon();

				if (indiv.sex != null && indiv.sex.equalsIgnoreCase("M")) {
					genderIcon.setImageResource(R.drawable.gender_male);
				} else if (indiv.sex != null && indiv.sex.equalsIgnoreCase("F")) {
					genderIcon.setImageResource(R.drawable.gender_female);
				} else {
					genderIcon.setImageResource(R.drawable.gender_unknown);
				}

				TextView nameVw = new TextView(this.getContext());
				nameVw.setText(indiv.getName());
				nameVw.setTextSize(this.textSize);

				LinearLayout ll2 = new LinearLayout(this.getContext());
				ll2.setOrientation(LinearLayout.HORIZONTAL);
				ll2.addView(genderIcon);
				ll2.addView(nameVw);
				ll2.setOnClickListener(this);

				this.addView(ll2);
			}
		} else {
			TextView nameVw = new TextView(this.getContext());
			nameVw.setText("No Children");
			nameVw.setTextSize(this.textSize);
			this.addView(nameVw);
		}

		this.requestLayout();
	}

}
